$(document).ready(function() {

	$.ajax({
		url: "http://hiveminder.com/=/action/AboutMe.json",
		type: "POST",
		success: function(data, textStatus, jqXHR) {
			if(data.content.current_user.name != undefined) {
				//$("#usersName").html(data.content.current_user.name);
				$("#hm-tasks").hide();
				$("#authYes").show();
				$("#authNo").hide();
				$("#hmbd-text").focus();
			} else {
				$("#authYes").hide();
				$("#authNo").show();
				$("#address").focus();
			}
		},
		error: function(jqXHR, textStatus, errorThrown) {
			$("#wrapper").append("<div id='misc'>");
			$("#misc").addClass('fail').html(textStatus);
		},
		dataType: "json"
	});
	
	$("#login_submit").bind("click", function() {
		var remYN;
		if($("#remember").is(":checked")) {
			remYN = 1;
		} else {
			remYN = 0;
		}
		
		var formData = {	address:	$("#address").val(),
							password:	$("#password").val(),
							remember:	remYN};
		
		$.ajax({
			url: "http://hiveminder.com/=/action/Login.json",
			type: "POST",
			data: formData,
			success: function(data, textStatus, jqXHR) {
				if(data.failure==1) {
					$("#loginStatus").html(data.error);
				} else {
					$("#loginStatus").html(data.message);
					setTimeout("window.location.reload()", 1000);
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$("#loginStatus").html("There was an error processing your request.");
			},
			dataType: "json"
		});
	});
	
	$("#hmbd-submit").bind("click", function() {
		$("#hmbd-status").removeClass("success").removeClass("fail").html("");
		$("#hmbd-status").show().html("<img src='images/ajax-loader.gif' />");
		var formData = {	text:	$("#hmbd-text").val(),
							tokens:	$("#hmbd-tokens").val()};

		$.ajax({
			url: "http://hiveminder.com/=/action/ParseTasksMagically.json",
			type: "POST",
			data: formData,
			success: function(data, textStatus, jqXHR) {
				$("#hmbd-status").addClass('success').html("Success!").delay(3000).fadeOut(1000);
				$("#hmbd-text").val("");
				setTimeout('window.close();', 1000);
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$("#hmbd-status").addClass('fail').html(textStatus);
			},
			dataType: "json"
		});
	});
	
	$("span.checkMark").live({
		click: function() {
			var info = this.id.substring(5);
			var dashLoc = info.search("-");
			var taskID = info.substring(0,dashLoc);
			var recordLocator = info.substring(dashLoc+1);
			var theURL = "http://hiveminder.com/=/model/Task/id/"+taskID+".json"
			var formData = {	complete: '1' }
			
			$.ajax({
				url:	theURL,
				type:	"PUT",
				data:	formData,
				success:	function(data, textStatus, jqXHR) {
					$("#task-"+recordLocator).addClass('taskComplete').delay(1000).hide(1000);
				},
				error:	function(jqXHR, textStatus, errorThrown) {
					$("#hm-tasks-status").append("<p>"+errorThrown+"</p>");
				},
				dataType:	"json"
			});
			
		},
		mouseenter: function() {
			$(this).addClass("isDone").removeClass("notDone");
		},
		mouseleave: function() {
			$(this).addClass("notDone").removeClass("isDone");
		}
	});
	
	$("#tasks-tab").bind("click", function() {
		getTasks();
		$("#hmbd").hide();
		$("#hm-tasks").show();
	});
	
	$("#bd-tab").bind("click", function() {
		$("#hm-tasks").hide();
		$("#hmbd").show();
		$("#hmbd-text").focus();
	});
});

function getTasks()
{
	$("#hm-tasks").html("");
	$.ajax({
		url:	"http://hiveminder.com/=/search/Task/complete/0.json",
		type:	"GET",
		success:	function(data, textStatus, jqXHR) {
			for (var i in data) {
				$("#hm-tasks").append("<div id='task-"+data[i].record_locator+"'></div>");
				var txtLine = "<span class='checkMark notDone' id='task-"+data[i].id+"-"+data[i].record_locator+"'></span>";
				txtLine = txtLine + "<a href='http://hiveminder.com/task/"+data[i].record_locator+"/edit' class='"+isOverdue(data[i].due)+"' target=_blank>"+data[i].summary+"</a><br />";
				$("#task-"+data[i].record_locator).append(txtLine);
			}
			$("#hm-tasks").append("<div id='hm-tasks-status'></div>");
		},
		error:	function(jqXHR, textStatus, errorThrown) {
			alert(textStatus);
		},
		dataType:	"json"
	});
}

function  isOverdue(taskDate)
{
	var curdate = new Date();
	var today = new Date(curdate.getFullYear(), curdate.getMonth(), curdate.getDate());
	var theClass;
	
	console.log(taskDate);
	
	if(!taskDate) {
		return "dueFuture";
	}
	else
	{
		taskDate = new Date(taskDate);
	}
	
	if((taskDate - today) == 0) {
		theClass = "dueToday";
	}
	else if((taskDate - today) < 0)
	{
		theClass = "overdue";
	}
	else if((taskDate - today) > 0)
	{
		theClass = "dueFuture";
	}
	
	return theClass;
}